package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.entity.StudentAccount;
import com.xmy.cultivate.entity.response.AiSitePc;
import com.xmy.cultivate.mapper.AppointSchoolMapper;
import com.xmy.cultivate.service.IAiSitePcService;
import com.xmy.cultivate.service.ISchoolQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电脑管理 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-28
 */
@RestController
@RequestMapping("/api/aiSitePc")
public class AiSitePcController extends BaseController {

    @Autowired
    IAiSitePcService aiSitePcService;

    @Autowired
    @Lazy
    AppointSchoolMapper appointSchoolMapper;

    @Autowired
    ISchoolQuarterService schoolQuarterService;


    /**
     * 获取校区场地
     */
    @GetMapping("/aiSchoolSiteList")
    public Result getAiSchoolSiteList(@RequestParam(value = "yearPart", required = false) String yearPart,
                                      @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                      @RequestParam(value = "schoolId", required = false) String schoolId) {
        // 判断是否为超级管理员
        if (!this.adminId.equals(1568138350324350975L)) {
            // 获取有权限的校区
            List<Long> schoolIdList = appointSchoolMapper.getSchoolIdListForForId(this.adminId, 2);
            // 根据校区查询出大校区
            if (schoolIdList != null && schoolIdList.size() > 0) {
                StringBuffer schoolIdStr = new StringBuffer();
                for (Long schoolIdItem : schoolIdList) {
                    schoolIdStr.append(schoolIdItem).append(",");
                }
                schoolId = schoolIdStr.substring(0, schoolIdStr.length() - 1);
                schoolId = schoolQuarterService.getBigSchoolIdStr(yearPart, quarterNum, schoolId);
            }

        }

        List<SchoolQuarter> aiSchoolSiteList = aiSitePcService.aiSchoolSiteList(yearPart, quarterNum, schoolId);

        return new Result(Code.OK, aiSchoolSiteList, Code.GET_OK_MSG);
    }

    /**
     * 获取电脑管理 使用情况列表
     */
    @GetMapping("/useConditionList")
    public Result getSuspensionRate(@RequestParam(value = "status", required = false) String status,// 电脑状态：1正常，2维修中，3报废
                                    @RequestParam(value = "useStatus", required = false) String useStatus,// 0空闲，1待使用，2使用中
                                    @RequestParam(value = "studentName", required = false) String studentName,
                                    @RequestParam(value = "schoolId", required = true) String schoolId,
                                    @RequestParam(value = "schoolSiteId", required = true) String schoolSiteId) {

        Map<String, Object> map = aiSitePcService.useConditionList(status, useStatus, studentName, schoolId, schoolSiteId);

        return new Result(Code.OK, map, Code.GET_OK_MSG);
    }

    /**
     * 调整电脑使用情况
     */
    @PostMapping("/updateComputerUse")
    public Result updateComputerUse(@RequestBody AiSitePc aiSitePc) {

        aiSitePc.setCreatedId(this.adminId);

        String code = aiSitePcService.updateComputerUse(aiSitePc);

        return new Result(Code.OK, code, "");
    }

    /**
<<<<<<< HEAD
     * 账号列表
=======
     * 查询维修原因
     * @param id
     */
    @GetMapping("/getRepairReason")
    public Result getRepairReason(@RequestParam(value = "id", required = true) Long id) {

        Map<String, Object> repairReason = aiSitePcService.getRepairReason(id);
        return new Result(Code.OK, repairReason, Code.GET_OK_MSG);
    }


    /**
     * 帐号列表
>>>>>>> origin/dev_likeshu
     */
    @GetMapping("/studentAccountList")
    public Result studentAccountList(@RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId,
                                     @RequestParam(value = "studentName", defaultValue = "", required = false) String studentName,
                                     @RequestParam(value = "account", defaultValue = "", required = false) String account,
                                     @RequestParam(value = "yearPart", defaultValue = "", required = false) String yearPart,
                                     @RequestParam(value = "quarterNum", defaultValue = "", required = false) String quarterNum) {

        List<StudentAccount> studentAccountList = aiSitePcService.studentAccountList(schoolId, studentName, account, yearPart, quarterNum);

        return new Result(Code.OK, studentAccountList, Code.GET_OK_MSG);
    }

}
